package com.wawei.seeds.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.igexin.sdk.PushConsts;
import com.igexin.sdk.PushManager;
import com.wawei.seeds.me.MeMainActivity;

import org.json.JSONObject;

public class PushGetuiReceiver extends BroadcastReceiver {
    public PushGetuiReceiver() {
    }

    /**
     * 应用未启动, 个推 service已经被唤醒,保存在该时间段内离线消息(此时 GetuiSdkDemoActivity.tLogView == null)
     */
    public static StringBuilder payloadData = new StringBuilder();
    public static final String APPOINTMENT_ME = "bookingApply";
    public static final String ME_APPOINTMENT_ACCEPT = "bookingAccept";
    public static final String ME_APPOINTMENT_REJECT = "bookingReject";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Bundle bundle = intent.getExtras();
        Log.d("test", "onReceive() action=" + bundle.getInt(PushConsts.CMD_ACTION));

        switch (bundle.getInt(PushConsts.CMD_ACTION)) {
            case PushConsts.GET_MSG_DATA:
                // 获取透传数据
                // String appid = bundle.getString("appid");
                byte[] payload = bundle.getByteArray("payload");

                //String taskid = bundle.getString("taskid");
                //String messageid = bundle.getString("messageid");

                // smartPush第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
                // boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
                // System.out.println("第三方回执接口调用" + (result ? "成功" : "失败"));

                if (payload != null) {
                    String data = new String(payload);

                    Log.d("test", "receiver payload : " + data);

                    try {
                        JSONObject object = new JSONObject(data);

                        String action = object.optString("action");

                        if (APPOINTMENT_ME.equals(action) || ME_APPOINTMENT_ACCEPT.equals(action) || ME_APPOINTMENT_REJECT.equals(action)) {
                            intent.putExtra("data", data);
                            intent.setClass(context, MeMainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                        }

                    } catch (Exception e) {

                    }

                    payloadData.append(data);
                    payloadData.append("\n");

                    // Toast.makeText(context, payloadData.toString(), Toast.LENGTH_LONG).show();
                }
                break;

            case PushConsts.GET_CLIENTID:
                // 获取ClientID(CID)
                // 第三方应用需要将CID上传到第三方服务器，并且将当前用户帐号和CID进行关联，以便日后通过用户帐号查找CID进行消息推送
                String cid = bundle.getString("clientid");

                // 这里实现与自己服务器接口的逻辑，我这里先显示数据事例
                // Toast.makeText(context, cid, Toast.LENGTH_LONG).show();

                Log.d("test", cid);
                break;

            case PushConsts.THIRDPART_FEEDBACK:
                /*
                 * String appid = bundle.getString("appid"); String taskid =
                 * bundle.getString("taskid"); String actionid = bundle.getString("actionid");
                 * String result = bundle.getString("result"); long timestamp =
                 * bundle.getLong("timestamp");
                 *
                 * Log.d("GetuiSdkDemo", "appid = " + appid); Log.d("GetuiSdkDemo", "taskid = " +
                 * taskid); Log.d("GetuiSdkDemo", "actionid = " + actionid); Log.d("GetuiSdkDemo",
                 * "result = " + result); Log.d("GetuiSdkDemo", "timestamp = " + timestamp);
                 */
                break;

            default:
                break;
        }
    }
}